using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using SMS.Common.ZipLib.Zip;
using SMS.Common.ZipLib.Checksums;

namespace SMS.Common.ZipLib
{
    class TestZip
    {
        void ZipOneFile()
        {
            Crc32 crc = new Crc32();
            ZipOutputStream outStream = new ZipOutputStream(File.Create("Employee.zip"));

            outStream.SetLevel(9); // 0 - store only to 9 - means best compression

            string file = "Employee.xml";

            FileStream fs = File.OpenRead(file);

            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            ZipEntry entry = new ZipEntry(file);

            entry.DateTime = DateTime.Now;

            // set Size and the crc, because the information
            // about the size and crc should be stored in the header
            // if it is not set it is automatically written in the footer.
            // (in this case size == crc == -1 in the header)
            // Some ZIP programs have problems with zip files that don't store
            // the size and crc in the header.
            entry.Size = fs.Length;
            fs.Close();

            crc.Reset();
            crc.Update(buffer);

            entry.Crc = crc.Value;

            outStream.PutNextEntry(entry);

            outStream.Write(buffer, 0, buffer.Length);

            outStream.Finish();
            outStream.Close();
        }

        void ZipMultiFiles()
        {
            string[] filenames = Directory.GetFiles("FolderName");

            Crc32 crc = new Crc32();
            ZipOutputStream outStream = new ZipOutputStream(File.Create("Employee.zip"));

            outStream.SetLevel(9); // 0 - store only to 9 - means best compression

            foreach (string file in filenames)
            {
                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(file);

                entry.DateTime = DateTime.Now;

                // set Size and the crc, because the information
                // about the size and crc should be stored in the header
                // if it is not set it is automatically written in the footer.
                // (in this case size == crc == -1 in the header)
                // Some ZIP programs have problems with zip files that don't store
                // the size and crc in the header.
                entry.Size = fs.Length;
                fs.Close();

                crc.Reset();
                crc.Update(buffer);

                entry.Crc = crc.Value;

                outStream.PutNextEntry(entry);

                outStream.Write(buffer, 0, buffer.Length);

            }

            outStream.Finish();
            outStream.Close();
        }

        void UnZipFiles(string[] args)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {

                Console.WriteLine(theEntry.Name);

                string directoryName = Path.GetDirectoryName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.Name);

                // create directory
                Directory.CreateDirectory(directoryName);

                if (fileName != String.Empty)
                {
                    FileStream streamWriter = File.Create(theEntry.Name);

                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }

                    streamWriter.Close();
                }
            }
            s.Close();
        }

    }
}
